package com.drinks.yiniang.adapter.community;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.entity.community.CommunityHotJson;
import com.drinks.yiniang.entity.community.CommunityNewsJson;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.ui.widget.textview.TagTextView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.Constants;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 热门社区列表adapter
 * @author han
 * @date 2019/8/30
 */
public class CommunityHotAdapter extends BaseQuickRCVAdapter<CommunityHotJson.CommunityHotList, BaseViewHolder> {

    private TagTextView mTagTextView;

    private Drawable mLikeNormalDrawable;
    private Drawable mLikeFocusDrawable;

    public CommunityHotAdapter(Context mContext, @Nullable List<CommunityHotJson.CommunityHotList> data) {
        super(R.layout.item_community_hot, data);

        mLikeNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_likes_mall_n);
        mLikeFocusDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_community_likes_mall_p);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommunityHotJson.CommunityHotList item) {
        //封面
        GlideUtils.loadSquareImage(mContext, (RatioRoundImageView) helper.getView(R.id.iv_community_hot_cover), item.getCover_url());
        //活动tag
        addLabelTag((AutoLineFeedLayout) helper.getView(R.id.ll_community_hot_label), item.getActivities());
        //是否精华
        helper.setGone(R.id.iv_community_hot_seal, Constants.STATUS_GLOBAL_YES.equals(item.getIs_essence()));
        //标题
        helper.setText(R.id.tv_community_hot_title, item.getTitle());
        //------用户
        //头像
        GlideUtils.loadAvatar(mContext, (CircleImageView) helper.getView(R.id.iv_community_hot_avatar), item.getAvatar());
        //昵称
        helper.setText(R.id.tv_community_hot_nickname, item.getUsername());
        //点赞数
        setLikeData((TextView) helper.getView(R.id.tv_community_hot_like), item);

        //是否来自关注
        if(Constants.STATUS_GLOBAL_YES.equals(item.getFrom_focus())) {
            helper.setGone(R.id.tv_community_hot_from_label, true);
        } else {
            helper.setGone(R.id.tv_community_hot_from_label, false);
        }

        helper.addOnClickListener(R.id.iv_community_hot_avatar, R.id.tv_community_hot_nickname);
        helper.addOnClickListener(R.id.tv_community_hot_like);
    }

    private void addLabelTag(AutoLineFeedLayout layout, List<GoodsJson.GoodsActivity> actList) {
        layout.removeAllViews();

        if (CommonUtils.isEmptyList(actList)) {
            layout.setVisibility(View.GONE);

            return;
        }

        layout.setVisibility(View.VISIBLE);

        for (GoodsJson.GoodsActivity data : actList) {
            mTagTextView = new TagTextView(mContext);

            mTagTextView.setTagColor(data.getTag_color());
            mTagTextView.setText(data.getTag_name());

            layout.addView(mTagTextView);
        }
    }

    //点赞
    private void setLikeData(TextView tvLike, CommunityHotJson.CommunityHotList item) {
        tvLike.setText(StringUtils.getNullEmptyConvertZero(item.getLike_count()));
        tvLike.setCompoundDrawablesWithIntrinsicBounds(Constants.STATUS_GLOBAL_YES.equals(item.getIs_like()) ? mLikeFocusDrawable : mLikeNormalDrawable, null, null, null);
    }
}
